define(['./../../service/chartsFactory', './../../service/loadProjectMap'], function () {
    var app = angular.module('App');
    app.controller('prjCtrl.change', ['$scope', 'viewbag', 'chartsFactory', 'loadProjectMap', 'defineTheme', function ($scope, viewbag, chartsFactory, loadProjectMap, defineTheme) {
        var curPrj = $scope.prj;
        //获取所有的测项和位置,合并成一个数组
        var monitorPoints = [];
        curPrj.monitorPoints.forEach(function (moniorPoint) {
            moniorPoint.locationPoints.forEach(function (lp, index) {
                lp.monitorType = moniorPoint.monitorType;
                lp.count = index === 0 ? moniorPoint.locationPoints.length : 0
                monitorPoints.push(lp);
            });
        });
        /** 所有locationPoints的集合数组 */
        $scope.monitorPoints = monitorPoints;
        /** 当前显示的监测类型的locationpoints合集 */
        $scope.activeMonitorPoints = curPrj.monitorPoints.length > 0 ? curPrj.monitorPoints[0] : [];//当前
        $scope.search = function () {
            var finish = 0;//当前完成查询w的数量
            $scope.busy = true;
            //获取所有的测量和位置的变化量
            $scope.monitorPoints.forEach(function (item, index) {
                curPrj.getRecentChanges({
                    type: item.monitorType,
                    location: item.location,
                    prjId: curPrj.prjId,
                    time1: $scope.startDate.date ? $scope.startDate.string : null,
                    time2: $scope.endDate.date ? $scope.endDate.string : null,
                    fuzzy: 1
                }).then(function (data) {
                    finish++;
                    //为两次时间相同情况下的speed做一个特殊处理
                    $scope.monitorPoints[index].changes = data;
                    if (!$scope.monitorPoints[index].noData && data.maxPointThisTime.speed === null) {
                        data.maxPointThisTime.speed = 0;
                    }
                    if (finish === $scope.monitorPoints.length) {//最后一个
                        $scope.busy = false;
                        $scope.noData = $scope.monitorPoints.every(function (item) {
                            return item.noData;
                        });
                        if (!$scope.noData) { //有数据的话
                            //当前应该显示chart的monitorpoints
                            $scope.activeMonitorPoints = curPrj.monitorPoints[0];
                            $scope.createChart();
                        }
                    }
                });
            });
            //防止没点还在等待查询
            if ($scope.monitorPoints.length === 0) {
                $scope.busy = false;
            }
        };

        /** 在全屏中查看布点图与变化曲线 */
        $scope.viewInFullscreen = function (lpts) {
            if (!$scope.prj.hasJsonLayer || !$scope.jsonMapComplete) return;
            removeAllActiveLocations();
            if (!$scope.fullscreen) {
                $scope.fullscreen = true;
                defineTheme.dark();
                lpts.active = true;
                $scope.activeLocationPoints = lpts;
                setTimeout(function () {
                    loadProjectMap.map.invalidateSize();//触发map的sizechange事件
                    loadProjectMap.renderPointsOnMap(lpts);//渲染当前的监测点
                    chartsFactory.createPointsChartByLocation('chart', lpts.changes, true);
                }, 20);
            } else {
                lpts.active = true;
                $scope.activeLocationPoints = lpts;
                loadProjectMap.renderPointsOnMap(lpts);//渲染当前的监测点
                chartsFactory.createPointsChartByLocation('chart', lpts.changes, true);
            }

        };


        function removeAllActiveLocations() {
            $scope.monitorPoints.some(function (l) {
                if (l.active) {
                    l.active = false;
                    return true;
                }
                return false;
            });
        }

        /** 退出全屏 */
        $scope.quitFullscreen = function () {
            $scope.fullscreen = false;
            removeAllActiveLocations();
        };

        /** 用户点击监测类型后,生成该类型下的所有图表 */
        $scope.createChart = function (type) {
            if (type) {
                $scope.activeMonitorPoints = curPrj.monitorPoints.filter(function (m) {
                    return m.monitorType === type;
                })[0];
            }
            defineTheme.light();
            setTimeout(function () {
                var chartDom = $('#chart0');
                if (type) {
                    $('.full-height-content').animate({
                        scrollTop: $('.full-height-content').scrollTop() + chartDom.offset().top - 200
                    }, 200, function () {
                        $scope.activeMonitorPoints.locationPoints.forEach(function (lpts, index) {
                            chartsFactory.createPointsChartByLocation('chart' + index, lpts.changes);
                        });
                    });
                } else {
                    $scope.activeMonitorPoints.locationPoints.forEach(function (lpts, index) {
                        chartsFactory.createPointsChartByLocation('chart' + index, lpts.changes);
                    });
                }
                toastr.success('已绘制所有' + $scope.activeMonitorPoints.monitorType + '图表', $scope.activeMonitorPoints.monitorType, { timeOut: 5000, "positionClass": "toast-bottom-right", "closeButton": true })
            }, 100);
        }


    }]);
});